package com.byedu.course.controller;

import com.byedu.course.util.ControllerUtil;
import com.byedu.domain.focus.FocusKpiDO;
import com.byedu.domain.focus.vo.enumvos.FocusdepaTypeEnum;
import com.byedu.domain.focus.vo.FocusKpiVO;
import com.byedu.domain.focus.vo.FocusUserVO;
import com.byedu.focus.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Luo on 2017-06-28.
 */
@Controller
@RequestMapping("/admin")
public class AdminIndexController {

    @Autowired
    private FocusProduceService focusProduceService;
    @Autowired
    private FocusUserService focusUserService;
    @Autowired
    private FocusKpiService focusKpiService;
    @Autowired
    private FocusProjectService focusProjectService;
    @Autowired
    private FocusLogService focusLogService;

    @RequestMapping("")
    public String defaultr(HttpServletRequest request,HttpServletResponse response) throws IOException {
        FocusUserVO adminUser = ControllerUtil.checkuser(request, response);
        String m = request.getParameter("m");
        if(StringUtils.isEmpty(m)){
            m = "user";
        }
//        System.out.println(adminUser);
//        System.out.println("getRemoteHost:"+ControllerUtil.getRemoteHost(request));
        Calendar a=Calendar.getInstance();
        request.setAttribute("m", m);
        List<FocusKpiVO> kpiList= null;
        FocusKpiDO focusKpiDO = null;
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
            kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, a.get(Calendar.YEAR)+"_1");
        }else {
            kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), a.get(Calendar.YEAR)+"_1");
        }
        focusKpiDO = focusKpiService.getkpiListSum(kpiList);

        request.setAttribute("firstsnum",focusKpiDO.getKipnum());
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
            kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, a.get(Calendar.YEAR)+"_2");
        }else {
            kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), a.get(Calendar.YEAR)+"_2");
        }
        focusKpiDO = focusKpiService.getkpiListSum(kpiList);
        request.setAttribute("secondnum",focusKpiDO.getKipnum());
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
            kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, a.get(Calendar.YEAR)+"_3");
        }else {
            kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), a.get(Calendar.YEAR)+"_3");
        }
        focusKpiDO = focusKpiService.getkpiListSum(kpiList);
        request.setAttribute("thridnum",focusKpiDO.getKipnum());
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
            kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, a.get(Calendar.YEAR)+"_4");
        }else {
            kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), a.get(Calendar.YEAR)+"_4");
        }
        focusKpiDO = focusKpiService.getkpiListSum(kpiList);
        request.setAttribute("fourthnum",focusKpiDO.getKipnum());
        return "index";
    }
    @RequestMapping("/index")
    public String index(HttpServletRequest request,HttpServletResponse response) throws IOException {

        FocusUserVO adminUser = ControllerUtil.checkuser(request, response);
        String m = request.getParameter("m");
        if(StringUtils.isEmpty(m)){
            m = "user";
        }
        Calendar a=Calendar.getInstance();
        request.setAttribute("m", m);
        List<FocusKpiVO> kpiList= null;
        FocusKpiDO focusKpiDO = null;
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
            kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, a.get(Calendar.YEAR)+"_1");
        }else {
            kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), a.get(Calendar.YEAR)+"_1");
        }
        focusKpiDO = focusKpiService.getkpiListSum(kpiList);

        request.setAttribute("firstsnum",focusKpiDO.getKipnum());
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
            kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, a.get(Calendar.YEAR)+"_2");
        }else {
            kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), a.get(Calendar.YEAR)+"_2");
        }
        focusKpiDO = focusKpiService.getkpiListSum(kpiList);
        request.setAttribute("secondnum",focusKpiDO.getKipnum());
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
            kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, a.get(Calendar.YEAR)+"_3");
        }else {
            kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), a.get(Calendar.YEAR)+"_3");
        }
        focusKpiDO = focusKpiService.getkpiListSum(kpiList);
        request.setAttribute("thridnum",focusKpiDO.getKipnum());
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
            kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, a.get(Calendar.YEAR)+"_4");
        }else {
            kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), a.get(Calendar.YEAR)+"_4");
        }
        focusKpiDO = focusKpiService.getkpiListSum(kpiList);
        request.setAttribute("fourthnum",focusKpiDO.getKipnum());
        return "index";
    }

}
